package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    //分页查询
    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listClues(new Page<Clue>(clueDto.getPage(),clueDto.getPageSize()), clueDto);
        return new PageResult<Clue>(page.getTotal(),page.getRecords());
    }

    //根据ID查询
    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    //线索跟进

    @Transactional(rollbackFor = Exception.class)//事物控制
    @Override
    public void trackClue(Clue clue) {
        //1.根据ID更新基本信息
        clue.setStatus(3);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //2.添加跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());//线索ID
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//跟进人ID
        clueTrackRecord.setSubject(clue.getSubject());//
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());

        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    //伪线索
    @Transactional(rollbackFor = Exception.class)//事物控制
    @Override
    public void markFalseClue(Integer id, MarkFalseClueDto markFalseClueDto) {

        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //添加跟进记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());

        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    //转商机
    @Transactional(rollbackFor = Exception.class)//事物控制
    @Override
    public void toBusiness(Integer id) {
        //根据ID更新线索状态
        Clue clue = clueMapper.selectById(id);

        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //根据线索表往商机表中添加数据
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId( null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId( id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert(business);
    }

    //线索池列表查询

    @Override
    public PageResult<Clue> Clues(ClueDto clueDto) {
        //base64只支持单表查询,分页查询需要使用MybatisPlus的分页插件,
        // 分页查询的时候涉及到多表查询,所以起名不能与Mybatis方法重名
        //只需要new一个Page对象,并传入分页页码和分页大小即可,再将实体类传入
        //这里返回的是Page对象,里面有分页数据
        Page<Clue> page= clueMapper.Clues(new Page<>(clueDto.getPage(),clueDto.getPageSize()),clueDto);
        //最终给Controller返回的是PageResult对象
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
}
